home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / c / rand < prev    next >
Text File  |  1996-11-09  |  2KB  |  96 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/c/RCS/rand,v $
  4.  * $Date: 1996/04/19 21:26:42 $
  5.  * $Revision: 1.1 $
  6.  * $State: Rel $
  7.  * $Author: simon $
  8.  *
  9.  * $Log: rand,v $
  10.  * Revision 1.1  1996/04/19 21:26:42  simon
  11.  * Initial revision
  12.  *
  13.  ***************************************************************************/
  14.  
  15. static const char rcs_id[] = "$Id: rand,v 1.1 1996/04/19 21:26:42 simon Rel $";
  16.  
  17. #include <stdlib.h>
  18.  
  19. static unsigned long __state[32] =
  20. {
  21.   0x6fdb9cb7, 0x9de8dc3d, 0x093bf9e4, 0x47528c2b, 0xfc263867, 0x53cbf1bf,
  22.   0x13618c92, 0x9e0f31b1, 0xcd651ab0, 0x2b52a7e5, 0x2ccdd9bf, 0x30052e2e,
  23.   0xb278be81, 0xd634a58b, 0x0a33d2c1, 0xfd42f052, 0xcb2f06f8, 0xa57bb730,
  24.   0x4ca963ac, 0x84bf5532, 0xd67ab9e6, 0x6e2d017b, 0x1e17cd99, 0x5891173a,
  25.   0x39384a29, 0xe0a0282e, 0x2e5512fc, 0x3093f269, 0x3a6983e6, 0x6b9fdaf3,
  26.   0x38b6bbd1, 0xb5e23046
  27. };
  28. static int __st1 = 0, __st2 = 3;
  29.  
  30. void
  31. srand (register long seed)
  32.  
  33. {
  34.   register int i;
  35.  
  36.   for (i = 0; i < 32; i++)
  37.     seed = __state[i] = (seed * 1103515245 + 12345);
  38.   __st1 = 0;
  39.   __st2 = 3;
  40.   for (i = 0; i < ((lrand () ^ seed) & 255); i++);
  41.   for (i = 0; i < ((lrand () ^ seed) & 255); i++)
  42.     lrand ();
  43. }
  44.  
  45. long
  46. lrand (void)
  47.  
  48. {
  49.   register long i, j;
  50.   register int k, l;
  51.  
  52.   i = *((long *) (__state + (k = __st1)));
  53.   j = *((long *) (__state + (l = __st2)));
  54.   if (i < 0 && j < 0)
  55.     i = -i;
  56.   __state[l] = (i += j);
  57.   __st1 = (k + 1) & 31;
  58.   __st2 = (l + 1) & 31;
  59.   return ((i >> 1) & RAND_MAX);
  60. }
  61.  
  62. int
  63.   (rand) (void)
  64.  
  65. {
  66.   return (rand ());
  67. }
  68.  
  69. void
  70.   (srand48) (register long seed)
  71.  
  72. {
  73.   srand48 (seed);
  74. }
  75.  
  76. long
  77.   (lrand48) (void)
  78.  
  79. {
  80.   return (lrand48 ());
  81. }
  82.  
  83. void
  84.   (srandom) (register long seed)
  85.  
  86. {
  87.   srandom (seed);
  88. }
  89.  
  90. long
  91.   (random) (void)
  92.  
  93. {
  94.   return (random ());
  95. }
  96.